package com.shop.shoporder.wapController;

import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.PassToken;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.Category;
import com.shop.shoporder.entity.WapClassificationGoodsListToData;
import com.shop.shoporder.entity.WapClassificationGoodsToData;
import com.shop.shoporder.entity.WapClassificationToData;
import com.shop.shoporder.service.WapClassificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@Controller
@RequestMapping("/wap")
public class WapClassificationController {

    Logger logger= LoggerFactory.getLogger(WapGoodsAndSpecController.class);

    @Autowired
    WapClassificationService wapClassificationService;


    /**
     * 查询一级分类
     *
     * @param request
     * @return
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/selectOneLevelByPId")
    public Response selectOneLevelByPId(HttpServletRequest request) {
        List<WapClassificationToData> classificationToDataList = null;
        try {
            classificationToDataList = wapClassificationService.selectOneLevelByPId();
            if (classificationToDataList != null && classificationToDataList.size() != 0) {
                return new Response(MessageConstants.MSG_SUCCESS, classificationToDataList, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }


    }


    /**
     * 查询二级分类
     *
     * @param request
     * @return
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/selectTwoLevelByPId")
    public Response selectTwoLevelByPId(HttpServletRequest request) {
        List<WapClassificationToData> classificationToDataList = null;
        try {
            classificationToDataList = wapClassificationService.selectTwoLevelByPId();
            if (classificationToDataList != null && classificationToDataList.size() != 0) {
                return new Response(MessageConstants.MSG_SUCCESS, classificationToDataList, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }


    }


    /**
     * 根据二级分类ID查询商品
     * @param request
     * @return
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/getGoodsByClassId")
    public Response getGoodsByClassId(HttpServletRequest request) {
        List<WapClassificationGoodsListToData> wapClassificationGoodsDataList = null;

        int cfId2 = 0;
        int userNoId = 0;
        int rankId = 0;
        String cfId = request.getParameter("cfId2");
        String userNoId1 = request.getParameter("userNoId");
        String rankId1 = request.getParameter("rankId");


        if (cfId != null && cfId != "") {
            cfId2 = Integer.parseInt(cfId);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }


        if (userNoId1 != null && userNoId1 != "") {
            userNoId = Integer.parseInt(userNoId1);
        }


        if (rankId1 != null && rankId1 != "") {
            rankId = Integer.parseInt(rankId1);
        }


        try {
            wapClassificationGoodsDataList = wapClassificationService.selectGoodsByCfId2(cfId2);
            List<WapClassificationGoodsToData> dataList = new ArrayList<>();

                WapClassificationGoodsToData wapClassificationGoodsToData = null;
                for (int i = 0; i < wapClassificationGoodsDataList.size(); i++) {
                    wapClassificationGoodsToData = new WapClassificationGoodsToData();
                    wapClassificationGoodsToData.setId(wapClassificationGoodsDataList.get(i).getId());
                    logger.info("分类列表goodsId:-------->"+wapClassificationGoodsDataList.get(i).getId());
                    wapClassificationGoodsToData.setCfId2(wapClassificationGoodsDataList.get(i).getCfId2());
                    wapClassificationGoodsToData.setName(wapClassificationGoodsDataList.get(i).getName());
                    wapClassificationGoodsToData.setNameDesc(wapClassificationGoodsDataList.get(i).getNameDesc());
                    wapClassificationGoodsToData.setPic(wapClassificationGoodsDataList.get(i).getPic());
                    wapClassificationGoodsToData.setSpecId(wapClassificationGoodsDataList.get(i).getSpec().getId());
                    logger.info("分类列表：specId-------->"+wapClassificationGoodsDataList.get(i).getSpec().getId());
                    wapClassificationGoodsToData.setPrice(wapClassificationGoodsDataList.get(i).getSpec().getPrice());
                    wapClassificationGoodsToData.setVouchers(wapClassificationGoodsDataList.get(i).getSpec().getVouchers());
                    wapClassificationGoodsToData.setPackSpec(wapClassificationGoodsDataList.get(i).getSpec().getPackSpec());
                    wapClassificationGoodsToData.setZoneId(wapClassificationGoodsDataList.get(i).getZoneId());
                    wapClassificationGoodsToData.setTitle(wapClassificationGoodsDataList.get(i).getTitle());

                    switch (rankId) {
                        case 3:
                            wapClassificationGoodsToData.setOwner(wapClassificationGoodsDataList.get(i).getSpec().getOwner1());
                            break;
                        case 4:
                            wapClassificationGoodsToData.setOwner(wapClassificationGoodsDataList.get(i).getSpec().getOwner2());
                            break;
                        case 5:
                            wapClassificationGoodsToData.setOwner(wapClassificationGoodsDataList.get(i).getSpec().getOwner3());
                            break;
                        case 6:
                            wapClassificationGoodsToData.setOwner(wapClassificationGoodsDataList.get(i).getSpec().getOwner4());
                            break;
                        default:
                            wapClassificationGoodsToData.setOwner(0.00);
                            break;
                    }
                    dataList.add(wapClassificationGoodsToData);
                }


                return new Response(MessageConstants.MSG_SUCCESS, dataList, 0, "", request);




        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }


    }



    /*查询一级和二级分类*/

    @PassToken
    @ResponseBody
    @RequestMapping("/getClassification")
    public Response getClassification(HttpServletRequest request) {
        List<Category> categoryList = null;

        try {

            categoryList = wapClassificationService.getCategoryList();
            if (categoryList != null && categoryList.size() != 0) {
                return new Response(MessageConstants.MSG_SUCCESS, categoryList, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
            }


        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }


    }


}
